﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Facade.Manager.Abstract;

using DataAccess.Repositories;

using Domain.Model;

namespace Facade.Manager
{
    public class SubprojectManager : AbstractManager
    {
        #region Members

        private static readonly SubprojectManager instance = new SubprojectManager();

        // SubprojectRepository
        private SubprojectRepository subprojectRepository;

        // ProjectRepository
        private ProjectRepository projectRepository;

        #endregion

        #region Properties

        #endregion

        #region SubprojectManager

        private SubprojectManager()
        {
            // instantiate SubprojectRepository
            subprojectRepository = new SubprojectRepository();

            // instantiate ProjectRepository
            projectRepository = new ProjectRepository();
        }

        public static SubprojectManager Instance
        {
            get 
            {
                return instance;
            }
        }

        #endregion

        #region public methods

        public Subproject GetSubprojectById(int id)
        {
            return subprojectRepository.GetById(id);
        }

        public void UpdateSubproject(int projectId, Subproject subproject)
        {
            var project = projectRepository.GetById(projectId);
            subproject.Project = project;

            subprojectRepository.Update(subproject);
        }

        public void DeleteSubproject(int id)
        {
            var subproject = subprojectRepository.GetById(id);
            var project = projectRepository.GetById(subproject.Project.Id);
            if (subproject != null && project != null)
            {
                subprojectRepository.Delete(subproject);

                project.RemoveSubProject(subproject);
            }
        }

        public Boolean AddSubprojectTask(int subprojectId, SubprojectTask task)
        {
            var subproject = subprojectRepository.GetById(subprojectId);
            if (subproject != null)
            {
                subproject.AddTask(task);
                subprojectRepository.Update(subproject);

                return true;
            }

            return false;
        }

        public SubprojectTask GetSubprojectTaskById(int id)
        {
            return subprojectRepository.GetTaskById(id);
        }

        public Boolean UpdateSubprojectTask(int subprojectId, SubprojectTask task)
        {
            var subproject = subprojectRepository.GetById(subprojectId);
            if (subproject != null)
            {
                subproject.UpdateTask(task);
                subprojectRepository.Update(subproject);

                return true;
            }

            return false;
        }

        public Boolean DeleteSubprojectTask(int subprojectId, SubprojectTask task)
        {
            var subproject = subprojectRepository.GetById(subprojectId);
            if (subproject != null)
            {
                subproject.RemoveTask(task);
                subprojectRepository.DeleteSubprojectTask(task);

                return true;
            }

            return false;
        }

        #endregion

        #region private methods

        #endregion 

        #region internal methods

        /// <summary>
        /// Delete SubprojectDetail with specific name from
        /// all subprojects. 
        /// This method can only be called from other managers.
        /// </summary>
        /// <param name="name"></param>
        internal void DeleteDetailFromSubprojects(string name)
        {
            List<Subproject> subprojects = subprojectRepository.GetAll() as List<Subproject>;

            subprojectRepository.DeleteDetailFromSubprojects(name);

            foreach (var subproject in subprojects)
            {
                subproject.RemoveDetail(name);
            }
        }

        #endregion
    }
}